# _*_ coding utf-8 _*_
# FILENAME：bomDetailList.py
# DESCRIPTION: bom明细管理
# AUTH: jarvis zhang
# DATE: 2022/5/3110:53 上午
from PyQt5 import QtGui
from PyQt5.QtWidgets import *
from PyQt5.Qt import *
from sqlalchemy import desc
import qtawesome as qta

from components.BaseDetailWindow import BaseDetailWindow
from models.baseModel import get_session, delete
from models.factoryTransportTimeModel import FactoryTransportTimeDetailsModel


class FactoryTransportTimeDetail(BaseDetailWindow):
    __versionId = 0
    __title = ''

    def __init__(self, versionId, title):
        super(FactoryTransportTimeDetail, self).__init__()
        self.__init_ui()
        self.__versionId = versionId
        self.__title = title

    ''' UI '''
    def __init_ui(self):
        print('init ui')
        # 信号槽连接
        # self.btn_add.clicked.connect(self.add_click)
        # self.btn_close.clicked.connect(self.close)

    ''' Methods '''
    #  窗体show
    def show(self):
        super(FactoryTransportTimeDetail, self).show()
        self.setWindowTitle("厂内运转时间明细管理-" + self.__title)
        # 设置表头
        self.setTableViewHeaders()
        # 查询
        total = self.query_total()
        self.setTotalRecordCount(total)  # 总量
        self.recordQuery(0)  # page

        # 关闭窗体,释放资源
    def close(self):
        super(FactoryTransportTimeDetail, self).close()

    def setTableViewHeaders(self):
        self.tableeHeaderLabels = ['编号', '作业点', '从工厂到达地址到作业点时间（分钟）']
        self.dataModel.setHorizontalHeaderLabels(self.tableeHeaderLabels)
        # 设置列宽
        self.tableView.setColumnWidth(0, 50)
        self.tableView.setColumnWidth(1, 200)
        self.tableView.setColumnWidth(2, 400)
        #  设置代理按钮,[btnEdit, btnUpload, btnCal, btnView, btnDel]
        # delegateBtnIsHiddes = [False, False, True, False, False]
        # self.tableView.setItemDelegateForColumn(4, CustomButtonDelegate(self, delegateBtnIsHiddes))

    # 记录查询，重写
    def recordQuery(self, limitIndex):
        data = self.page(limitIndex)
        self.dataModel.clear()
        if len(data) > 0:
            for i in range(len(data)):
                # 添加
                newItem = QStandardItem(str(data[i].id))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 0, newItem)

                newItem = QStandardItem(str(data[i].work_site))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 1, newItem)

                newItem = QStandardItem(str(data[i].transport_time))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 2, newItem)

        self.setTableViewHeaders()

    # 分页查询列表
    def page(self, limitIndex=0):
        # 获取dbsession
        session = get_session()
        # 拼接查询
        my_query = session.query(FactoryTransportTimeDetailsModel)
        my_query = my_query.filter(FactoryTransportTimeDetailsModel.is_del == 0)
        my_query = my_query.filter(FactoryTransportTimeDetailsModel.factory_transport_time_id == self.__versionId)
        # 拼接排序条件
        my_query = my_query.order_by(desc(FactoryTransportTimeDetailsModel.update_date))
        # 分页
        my_query = my_query.offset(limitIndex).limit(self.PageRecordCount)
        # 执行查询
        model = my_query.all()
        session.close()
        return model

    # 查询数据总量
    def query_total(self):
        # 获取dbsession
        session = get_session()
        # 拼接查询
        my_query = session.query(FactoryTransportTimeDetailsModel)
        my_query = my_query.filter(FactoryTransportTimeDetailsModel.is_del == 0)
        my_query = my_query.filter(FactoryTransportTimeDetailsModel.factory_transport_time_id == self.__versionId)
        # 执行查询
        count = my_query.count()
        session.close()
        return count

    # 新增
    def add_click(self):
        print('add')
        # VersionDialog.get_dialog(self)

    # 编辑，继承父类
    def edit_click(self):
        super(FactoryTransportTimeDetail, self).edit_click()
        button = self.sender()
        if button:
            cell = button.index
            row = cell[0]
            id = self.dataModel.item(row, 0).text()
            # VersionDialog.get_dialog(self, int(id))

    #  上传，继承父类
    def upload_click(self):
        button = self.sender()
        if button:
            cell = button.index
            row = cell[0]
            id = self.dataModel.item(row, 0).text()
            # UploadDialog.get_dialog(self, id)
            print(row)

    #  查看，继承父类
    def view_click(self):
        print('view')
        # button = self.sender()
        # if button:
        #     cell = button.index
        #     row = cell[0]
        #     id = self.dataModel.item(row, 0).text()
        #     dialog = BomDetailList(self, int(id))
        #     dialog.show()

    # 删除，继承父类
    def del_click(self):
        choice = QMessageBox.question(self, '', '是否删除当前数据?', QMessageBox.Yes | QMessageBox.No)  # 1
        if choice == QMessageBox.Yes:  # 2
            button = self.sender()
            if button:
                cell = button.index
                row = cell[0]
                id = self.dataModel.item(row, 0).text()
                model = FactoryTransportTimeDetailsModel()
                model.id = int(id)
                delete(model)
                self.refresh()
        elif choice == QMessageBox.No:  # 4
            pass
